<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>注册</title>
    <link rel="stylesheet" href="/css/style.css"/>
</head>
<body>
<div class="center-container">
    <h1>应急图书管理系统 - 注册</h1>
    <form id="registerForm" class="center-form">
        <label>学号: <input type="text" name="username" required></label><br>
        <label>密码: <input type="password" name="password" required></label><br>
        <label>手机号: <input type="text" name="phone"></label><br>
        <label>姓名: <input type="text" name="name"></label><br>
        <label>邮箱: <input type="text" name="email"></label><br>
        <!-- 用户类型选择 -->
        <label>用户类型:
            <select name="userType" required>
                <option value="user">学生</option>
            </select>
        </label><br>
        <button type="submit">注册</button>
    </form>
    <p><a href="/user/login">已有账号？点击登录</a></p>
</div>
<script>
    document.getElementById('registerForm').addEventListener('submit', async (e) => {
        e.preventDefault();
        const formData = new FormData(e.target);
        const data = Object.fromEntries(formData);
        const response = await fetch('/user/register', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        });
        const result = await response.json();
        alert(result.message);
        if (result.code === 0) {
            window.location.href = '/user/login';
        }
    });
</script>
</body>
</html>